package Vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.ieorganic.bl.AdministradorBlLocal;
import mx.itschapala.dsw2.ieorganic.modelo.Administrador;

@ManagedBean
@SessionScoped
public class AdministradorBean {
    @EJB
    private AdministradorBlLocal administradorBl;

    
    private List<Administrador> lista;
    private Administrador administrator;
    private String accion;

    public AdministradorBean() {
        administrator=new Administrador();
    }

    public List<Administrador> getLista() {
        lista=administradorBl.getAll();
        System.out.println(lista);
        
        return lista;
    }
     public Administrador getAdministrador() {
        return administrator;
    }

    public void setAdministrador(Administrador administrator) {
        this.administrator = administrator;
    }

    public String procesarAccion(){
    if(accion.equalsIgnoreCase("nuevo")){
    administradorBl.register(administrator);
    }else{
    if(accion.equalsIgnoreCase("Editar")){
    administradorBl.modify(administrator);
    
    }else{
    if(accion.equalsIgnoreCase("Eliminar")){
    administradorBl.delete(administrator);
     }
    }
    }
    accion="";
    return "Administrador";

    }
    
    
 
public void prepararNuevo(ActionEvent e){
    administrator=new Administrador(); 
    accion="Nuevo";
}
public void prepararEditar(ActionEvent evt){
    System.out.println(evt.getComponent().getClientId());
    int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    System.out.println("el numero es:"+ index);
    administrator=lista.get(index);
    accion="Editar";               
}
public void prepararEliminar(ActionEvent ev){
    int index = Integer.parseInt(ev.getComponent().getClientId().split(":")[2]);      
    administrator=lista.get(index);
    accion="Eliminar";
}
}

